package com.scuvc.controller.admin;

import com.scuvc.entity.CarInfo;
import com.scuvc.utils.ImageUtils;
import com.scuvc.utils.JMenuBarUtils;
import com.scuvc.utils.StaticUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class QueryCarView extends JFrame {
    public void start(){
        setTitle("城市租车系统");
        //设置窗体起始位置
        setLocation(50,50);
        //设置窗体大小
        setSize(1270,670);
        //把图片添加到标签里
        ImageIcon img = new ImageIcon("src/images/bg_admin.jpg");
        ImageIcon bg = ImageUtils.createAlphaIcon(img,0.5f,
                img.getIconWidth(),img.getIconHeight());
        JLabel label = new JLabel(bg);
        //设置标签的大小
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        //把标签放在分层面板的最底层
        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //把窗口面板设为内容面板并设为透明，流动布局
        JPanel pan = (JPanel)getContentPane();
        pan.setOpaque(false);
        pan.setLayout(new BorderLayout());

        JPanel panCenter = new JPanel();
        FlowLayout fl = new FlowLayout();
        fl.setHgap(0);
        panCenter.setLayout(fl);
        panCenter.setOpaque(false);
        //设置标题栏
        setTop(pan,panCenter);
        //设置左边菜单栏
        setLeft(pan);
        //设置中间表格部分
        //边框
        panCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        panCenter.setOpaque(false);
        add(panCenter,BorderLayout.CENTER);
        //设置中间表格表头部分
        setCenterTitle(panCenter);
        //设置中间表格数据部分
        StaticUtils.refresh = setCenterData(panCenter);

        //分页
        JPanel panDown = new JPanel();
        panDown.setLayout(new FlowLayout());
        panDown.setOpaque(false);

        JButton firstPage = new JButton("首\u3000页");
        JButton endPage = new JButton("尾\u3000页");
        JButton prePage = new JButton("上一页");
        JButton nextPage = new JButton("下一页");
        add(panDown,BorderLayout.SOUTH);

        java.util.List<JButton> jbList = new ArrayList<>();
        //存储JButton按钮的集合
        for(int i=0;i<StaticUtils.pageBean.getTotalPage();i++){
            jbList.add(new JButton(i+1+""));
        }
        panDown.add(firstPage);
        panDown.add(prePage);
        //将集合中的JButton按钮添加到面板中,并根据button的点击监听事件完成数据跟新
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取一组指定按钮对象
                JButton jButton = (JButton) e.getSource();
                //获取按钮对应页码转为int类型
                int index = Integer.valueOf(jButton.getText());
                //清理对应原始页码的数据
                for(int i=0;i<StaticUtils.refresh.size();i++){
                    panCenter.remove(StaticUtils.refresh.get(i));
                }
                //更新pageBean对象中的数据
                StaticUtils.pageBean.setCurPage(index);
                StaticUtils.pageBean.setNextPage();
                StaticUtils.pageBean.setPrePage();
                StaticUtils.pageBean.setDataList(StaticUtils.carInfoService.getAll());
                StaticUtils.pageBean.setTotalPage();
                //查询获取更新数据
                java.util.List<CarInfo> list = StaticUtils.carInfoService.getAllPage(
                        StaticUtils.pageBean.getCurPage(),StaticUtils.pageBean.getPageSize());
                //创建存储JLabel对象的集合
                java.util.List<JLabel> carList = new ArrayList<>();
                //将跟新数据封装到JLabel对象中，并将JLabel添加到面板中
                JMenuBarUtils.setData(carList,list,panCenter,"admin");
                //记录当前最新数据
                StaticUtils.refresh = carList;
                //刷新页面
                repaint();
            }
        };
        for(int i=0;i<StaticUtils.pageBean.getTotalPage();i++){
            panDown.add(jbList.get(i));
            jbList.get(i).addActionListener(actionListener);
        }

        panDown.add(nextPage);
        panDown.add(endPage);
        //添加首页、尾页的监听事件
        ActionListener actionListener1 = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton jButton = (JButton) e.getSource();
                String text = jButton.getText();
                int index = 1 ;
                if(text.equals("尾\u3000页")){
                    index = StaticUtils.pageBean.getTotalPage();
                }
                if(text.equals("上一页")){
                    index = StaticUtils.pageBean.getCurPage()-1;
                }else if(text.equals("下一页")){
                    index = StaticUtils.pageBean.getCurPage()+1;
                }
                //清理对应原始页码的数据
                for(int i=0;i<StaticUtils.refresh.size();i++){
                    panCenter.remove(StaticUtils.refresh.get(i));
                }
                //更新pageBean对象中的数据
                StaticUtils.pageBean.setCurPage(index);
                StaticUtils.pageBean.setNextPage();
                StaticUtils.pageBean.setPrePage();
                StaticUtils.pageBean.setDataList(StaticUtils.carInfoService.getAll());
                StaticUtils.pageBean.setTotalPage();
                //查询获取更新数据
                java.util.List<CarInfo> list = StaticUtils.carInfoService.getAllPage(
                        StaticUtils.pageBean.getCurPage(),StaticUtils.pageBean.getPageSize());
                //创建存储JLabel对象的集合
                java.util.List<JLabel> carList = new ArrayList<>();
                //将跟新数据封装到JLabel对象中，并将JLabel添加到面板中
                JMenuBarUtils.setData(carList,list,panCenter,"admin");
                //记录当前最新数据
                StaticUtils.refresh = carList;
                //刷新页面
                repaint();
            }
        };
        firstPage.addActionListener(actionListener1);
        endPage.addActionListener(actionListener1);
        prePage.addActionListener(actionListener1);
        nextPage.addActionListener(actionListener1);
        //设置窗体大小是否可调整
        setResizable(false);
        //设置窗体是否可见
        setVisible(true);
        //单击关闭按钮执行的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private java.util.List<JLabel> setCenterData(JPanel panCenter) {
        StaticUtils.carList = StaticUtils.carInfoService.getAllPage(
                StaticUtils.pageBean.getCurPage(),StaticUtils.pageBean.getPageSize());
        java.util.List<JLabel> list = new ArrayList<>();
        for(int i=0;i<StaticUtils.carList.size();i++){
            list.add(new JLabel(StaticUtils.carList.get(i).getId()+""));
            list.add(new JLabel(StaticUtils.carList.get(i).getCarNo()+""));
            list.add(new JLabel(StaticUtils.carList.get(i).getBrand().getBrand_name()));
            list.add(new JLabel(StaticUtils.carList.get(i).getModel().getModel_name()));
            list.add(new JLabel(StaticUtils.carList.get(i).getCategory().getCategory_name()));
            list.add(new JLabel(StaticUtils.carList.get(i).getDisplacement()));
            list.add(new JLabel(StaticUtils.carList.get(i).getPrice()+""));
            list.add(new JLabel(StaticUtils.carList.get(i).getCar_amount()+""));
            list.add(new JLabel(StaticUtils.carList.get(i).getDeposit()+""));
            list.add(new JLabel(StaticUtils.carList.get(i).getStatus()==1?"可租":"已租"));
            list.add(new JLabel(StaticUtils.carList.get(i).getUsable()==1?"上架":"下架"));
        }
        Font font = new Font("仿宋",Font.PLAIN,18);
        for(int i=0;i<list.size();i++){
            list.get(i).setFont(font);
            panCenter.add(list.get(i));
            //设置标签的边框线
            list.get(i).setBorder(BorderFactory.createLineBorder(Color.BLACK));
            //设置标签的列宽
            list.get(i).setPreferredSize(new Dimension(
                    108,40));
            list.get(i).setHorizontalAlignment(SwingConstants.CENTER);
        }
        return list ;
    }

    private void setCenterTitle(JPanel panCenter) {
        panCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        panCenter.setPreferredSize(new Dimension(panCenter.getWidth(),60));
        java.util.List<String> jLabelName = new ArrayList<>();
        jLabelName.add("编号");
        jLabelName.add("车牌号");
        jLabelName.add("品牌");
        jLabelName.add("模式");
        jLabelName.add("类别");
        jLabelName.add("排量");
        jLabelName.add("价格");
        jLabelName.add("租金");
        jLabelName.add("押金");
        jLabelName.add("租恁状态");
        jLabelName.add("上/下架");
        List<JLabel> list = new ArrayList<>();
        for(int i=0;i<jLabelName.size();i++){
            list.add(new JLabel(jLabelName.get(i)));
        }
        Font font = new Font("仿宋",Font.BOLD,24);
        for(int i=0;i<list.size();i++){
            list.get(i).setFont(font);
            panCenter.add(list.get(i));
            //设置标签的边框线
            list.get(i).setBorder(BorderFactory.createLineBorder(Color.BLACK));
            //设置标签的列宽
            list.get(i).setPreferredSize(new Dimension(108,60));
            list.get(i).setHorizontalAlignment(SwingConstants.CENTER);
        }
    }

    private void setLeft(JPanel pan) {
        JPanel panLeft = new JPanel();
        panLeft.setLayout(new GridLayout(0,1));
        panLeft.setOpaque(false);
        //将左边面板添加到面板的左边
        pan.add(panLeft,BorderLayout.WEST);
        java.util.List<String> jMenuName = new ArrayList<>();
        jMenuName.add("车辆管理");
        jMenuName.add("用户管理");
        jMenuName.add("个人中心");
        jMenuName.add("租恁管理");
        String[][] itemName = {
                {"添加车辆","修改车辆","查询车辆"},
                {"查询用户","重置密码","修改用户"},
                {"个人信息","修改密码"},
                {"查询记录","添加记录","修改记录"}};
        JMenuBar jMenuBar = JMenuBarUtils.createJMenuBar
                (jMenuName,65,0, itemName);
        panLeft.add(jMenuBar);
        add(panLeft,BorderLayout.WEST);

        ActionListener carManage = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JMenuItem jmi = (JMenuItem) e.getSource();
                String text = jmi.getText();
                if(text.equals("添加车辆")){
                    AddCarView acv = new AddCarView();
                    setVisible(false);
                    acv.addStart();
                }else if(text.equals("查询车辆")){
                    QueryCarView qcv = new QueryCarView();
                    setVisible(false);
                    qcv.start();
                }else if(text.equals("修改车辆")){
                    UpdateCarView ucv = new UpdateCarView();
                    setVisible(false);
                    ucv.updateStart();
                }
            }
        };
        StaticUtils.jmiList[0][0].addActionListener(carManage);
        StaticUtils.jmiList[0][1].addActionListener(carManage);
        StaticUtils.jmiList[0][2].addActionListener(carManage);

    }
    private void setTop(JPanel pan,JPanel panCenter) {
        JPanel panTop = new JPanel();
        GridLayout grid = new GridLayout(2,1);
        grid.setVgap(30);
        panTop.setLayout(grid);
        panTop.setOpaque(false);
        JLabel jTop = new JLabel("智能管家");
        jTop.setFont(new Font("仿宋",Font.BOLD,36));
        jTop.setHorizontalAlignment(SwingConstants.CENTER);
        jTop.setPreferredSize(new Dimension(jTop.getPreferredSize().width,40));
        panTop.add(jTop);

        JPanel panTop2 = new JPanel();
        GridLayout gril1 = new GridLayout(1,9);
        gril1.setHgap(10);
        panTop2.setLayout(gril1);
        panTop2.setOpaque(false);
        Font font = new Font("仿宋",Font.BOLD,20);
        JLabel j2L1 = new JLabel("车牌号查询：");
        j2L1.setFont(font);
        j2L1.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j2f2 = new JTextField();
        j2f2.setFont(font);

        JLabel j2L3 = new JLabel("品牌查询：");
        j2L3.setFont(font);
        j2L3.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j2f4 = new JTextField();
        j2f4.setFont(font);

        JLabel j2L5 = new JLabel("模式查询：");
        j2L5.setFont(font);
        j2L5.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j2f6 = new JTextField();
        j2f6.setFont(font);

        JLabel j2L7 = new JLabel("类别查询：");
        j2L7.setFont(font);
        j2L7.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j2f8 = new JTextField();
        j2f8.setFont(font);

        JButton jButton = new JButton("搜\u3000索");

        panTop2.add(j2L1);
        panTop2.add(j2f2);
        panTop2.add(j2L3);
        panTop2.add(j2f4);
        panTop2.add(j2L5);
        panTop2.add(j2f6);
        panTop2.add(j2L7);
        panTop2.add(j2f8);
        panTop2.add(jButton);
        panTop.add(panTop2);
        pan.add(panTop,BorderLayout.NORTH);
        JTextField[] jtf = {j2f2,j2f4,j2f6,j2f8};
        setAddActionListener(jButton,panCenter,jtf);
        //设置获取和失去焦点
        StaticUtils.setFoucusListener(j2f2);
        StaticUtils.setFoucusListener(j2f4);
        StaticUtils.setFoucusListener(j2f6);
        StaticUtils.setFoucusListener(j2f8);
    }
    private void setAddActionListener(JButton jButton,JPanel panCenter,JTextField...jtf) {
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //清理对应原始页码的数据
                for(int i=0;i<StaticUtils.refresh.size();i++){
                    panCenter.remove(StaticUtils.refresh.get(i));
                }
                String carNo = jtf[0].getText();
                String brand_name = jtf[1].getText();
                String model_name = jtf[2].getText();
                String category_name = jtf[3].getText();
                if(!carNo.equals("") && !brand_name.equals("") || !carNo.equals("") &&
                        !model_name.equals("") || !carNo.equals("") && !category_name.equals("")
                        || !brand_name.equals("") && !model_name.equals("") ||
                        !brand_name.equals("") && !category_name.equals("") ||
                        !model_name.equals("") && !category_name.equals("") ||
                        !carNo.equals("") && !brand_name.equals("") && !model_name.equals("") ||
                        !carNo.equals("") && !brand_name.equals("") && !category_name.equals("") ||
                        !carNo.equals("") && !category_name.equals("") && !model_name.equals("") ||
                        !category_name.equals("") && !brand_name.equals("") && !model_name.equals("") ||
                        !carNo.equals("") && !brand_name.equals("") && !model_name.equals("") &&
                                !category_name.equals("")){
                    JOptionPane.showMessageDialog(null, "请检查筛选条件是否单一",
                            "提示", JOptionPane.INFORMATION_MESSAGE);
                }else{
                    if(!carNo.equals("")){
                        StaticUtils.carList = StaticUtils.carInfoService.getCarInfoByCarNo(carNo);
                    }else if(!brand_name.equals("")){
                        //通过获取品牌名字查询对应id值
                        Long brand_id = StaticUtils.brandInfoService.getBrandId(brand_name);
                        //通过品牌id查询汽车信息
                        StaticUtils.carList = StaticUtils.carInfoService.getCarInfoByBrandId(brand_id);
                    }else if(!model_name.equals("")){
                        //通过获取模式名查询对应id值
                        Long model_id = StaticUtils.modelInfoService.getModelId(model_name);
                        //通过品牌id查询汽车信息
                        StaticUtils.carList = StaticUtils.carInfoService.getCarInfoByModelId(model_id);
                    }else if(!category_name.equals("")){
                        //通过获取类别名查询对应id值
                        Long category_id = StaticUtils.categoryInfoDao.getCategroyId(category_name);
                        //通过类别id查询汽车信息
                        StaticUtils.carList = StaticUtils.carInfoService.getCarInfoByCategroyId(category_id);
                    }
                }
                //创建存储JLabel对象的集合
                List<JLabel> carList = new ArrayList<>();
                //将跟新数据封装到JLabel对象中，并将JLabel添加到面板中
                JMenuBarUtils.setData(carList,StaticUtils.carList,panCenter,"admin");
                //记录当前最新数据
                StaticUtils.refresh = carList;
                //刷新
                repaint();
            }
        });

    }
}

